<?php
require_once('../libraryfiles/config.php');
$content = 'product_detail.php';
$pro_id = isset($_GET['product_id']) ? $_GET['product_id'] : '';
$include = '<script type="text/javascript" src="' . base_url . 'templates/javascripts/jquery.jcarousel.min.js"></script>';
$product = new prodcut_tb();
$product->getall_product($pro_id);
$desc_site = getmeta_tag('description', $pro_id);
$keyword_site = getmeta_tag('keyword', $pro_id);
$site_description = ((!empty($desc_site)) ? $desc_site : $product->product_name);
$site_keywords = ((!empty($site_description)) ? $site_description : $product->product_name);
$site_title = $product->product_name;
require_once TMP_USER_PATH;
$cat_id = isset($_GET['cat_id']) ? $_GET['cat_id'] : '';
$category = isset($_GET['cat_p']) ? $_GET['cat_p'] : $cat_id;

//statistic
$product->owner;

$update_view = mysql_query("UPDATE tblproduct SET top_view = top_view+1 WHERE id = $pro_id");
// check
$counter = mysql_query("SELECT * FROM counter WHERE pd_id=$pro_id");
$counter_product = mysql_num_rows($counter);
$count_all = mysql_num_rows($counter);

if (isset($_SESSION['username'])) {
    if ($counter_product == 0) {
        $counter_ins = "INSERT INTO counter (COUNT, pd_id, by_user) VALUES (1,$pro_id,'" . $_SESSION['acc_id'] . "');";
        $counter_in = mysql_query($counter_ins);
    } else {
        $update_counter = mysql_query("UPDATE counter SET count = count+1 WHERE pd_id = $pro_id");
    }
} else {
//for click from all
    if ($count_all == 0) {
        $user = $product->owner;
        $counter_all = "INSERT INTO counter(pd_id, by_user, count_all) VALUE($pro_id,$user,1)";
        $counter_ins = mysql_query($counter_all);
    } else {
        $counter_all = mysql_query("UPDATE counter SET count_all=count_all+1 WHERE pd_id = $pro_id");
    }

// end click from all
}
//end statistic

$test = mysql_query("SELECT
				p.id,p.product_name, LEFT(p.product_description,500) AS subdesc,img.thumbnail,images,pip.country_id
				FROM tblproduct p LEFT JOIN tblimage img ON  p.id=img.product_id
				LEFT JOIN tblproduct_in_pharmacy pip ON p.id = pip.product_id
				WHERE p.id=$pro_id GROUP BY pip.product_id");
$select = mysql_query("SELECT
				p.owner, p.id,p.top_view,pip.price,pip.regular_price,p.product_name,p.short_desciption,p.product_description,img.thumbnail,images,original_img,pip.country_id,p.pr_province_id, pip.pid,p.product_version,p.contact_name,p.contact_email,p.contact_phone,p.contact_location,p.contact_address
				FROM tblproduct p LEFT JOIN tblimage img ON  p.id=img.product_id
				LEFT JOIN tblproduct_in_pharmacy pip ON p.id = pip.product_id
				WHERE p.id=$pro_id GROUP BY pip.product_id");
$data = mysql_fetch_assoc($select);
$datatest = mysql_fetch_assoc($test);

/* get category */
$category_pro = select(Tblproductincat::TABLE, array(), array(Tblproductincat::product_id => $pro_id));
$cat_data = mysql_fetch_assoc($category_pro);
/* end get category */

//Company
//$user_check = select(Tbluser::Tbluser, array(Tbluser::user_type_id), array(Tbluser::id => $data[Tblproduct::owner]));
//$row_user = @mysql_fetch_assoc($user_check);
//$query = "SELECT p.pharmacy_id,p.shorturl,p.pharmacy_name FROM tblpharmacy p LEFT JOIN tblproduct_in_pharmacy pip ON p.user_id=pip.pharmacy_id WHERE pip.product_id=" . $data[Tblproduct::id];
//$result1 = @mysql_query($query);
//$getcompany = @mysql_query($query);
//$company = @mysql_fetch_assoc($getcompany);
//
//
//if ($row_user[Tbluser::user_type_id] == 2):
//    $com_link = $company[Tblcompany::shorturl];
//    $com_name = $company[Tblcompany::name];
//else:
//    $com_link = '';
//    $com_name = '';
//endif;
//Company
//user product

$user_pro = select(Tblproduct::tblproduct, '', array(Tblproduct::id => $data[Tblproduct::id]));
$pro_q = @mysql_fetch_assoc($user_pro);
$user_id = $pro_q[Tblproduct::owner];
$get_owner = select(Tbluser::Tbluser, '', array(Tbluser::id => $user_id));
$user_q = @mysql_fetch_assoc($get_owner);
if ($user_q[Tbluser::user_type_id] == 2):
    if (!empty($user_q)):
        $com_name = $user_q[Tbluser::first_name];
        $com_link = $user_q[Tbluser::id];
    endif;
else:
    $com_link = '';
    $com_name = '';
endif;
//end user product
?>
<div class="breadcrumb">
    <a class="lever_1" style="z-index:100" href="<?php echo base_url; ?>"><?php echo $lang['HOME']; ?></a>
    <a class="lever_2" style="z-index:99" href="<?php echo base_url; ?>product"><?php echo $lang['UR_PRO_LIST']; ?></a>
    <?php get_crumbs($cat_data[Tblproductincat::category_id], 0, $cat_data[Tblproductincat::category_id]); ?>
    <a class="lever_8" style="z-index:90" href="<?php echo $CURRENT_URL; ?>"><?php echo $data[Tblproduct::name]; ?></a>
</div>
<div class="row-fluid product-info">
    <?php include(S_ROOT . '/templates/sidebar-left.php'); ?>
    <style>.bold{color:black;font-size:14px;}</style>
    <div class="span12 category">
        <div id="content">
            <div class="title">
                <h1 class="category-title">
                    <?php echo $data[Tblproduct::name]; ?>
                </h1>
                <?php if (!empty($com_link)): ?>
                    <div class="product-compare"><a href="<?php echo base_url; ?>user.php?id=<?php echo $com_link; ?>" target="_blank" id="compare-total"><?php echo $com_name ?></a></div>
                    <div class="see-more"><i>see more product</i> </div>
                <?php endif; ?>
            </div>
            <div class="box-content">
                <div class="product-grid">
                    <div class="row-fluid">
                        <div class="span6 product-img-box image-container">
                            <div class="image-additional slide carousel"
                                 id="image-additional">
                                <div class="carousel-inner">
                                    <?php
                                    $where_img = array("product_id" => $data['id']);
                                    $sql_img = select("tblimage", array(), $where_img);
                                    $i = 1;
                                    while ($row_img = mysql_fetch_assoc($sql_img)) {
                                        ?>
                                        <?php if ($i % 3 == 1) { ?>
                                            <div class="item">
                                            <?php } ?>
                                            <a href="<?php echo W_S_IMAGES_DIR . $row_img["images"]; ?>" class="colorbox""><img
                                                    src="<?php echo W_S_IMAGES_DIR_THUMB . $row_img["images"]; ?>"
                                                    style="max-width: 106px"
                                                    title="<?php //echo $heading_title;           ?>"
                                                    alt="<?php //echo $heading_title;           ?>"
                                                    data-zoom-image="<?php echo $row_img["images"]; ?>"
                                                    class="product-image-zoom" />
                                            </a>
                                            <?php if ($i % 3 == 0) { ?>
                                            </div>
                                            <?php
                                        }
                                        $i++;
                                        ?>
                                        <?php
                                    }
                                    if ($i % 3 != 1)
                                        echo "</div>";
                                    ?>
                                </div>
                                <div class="carousel-control left" href="#image-additional"
                                     data-slide="prev">&lsaquo;</div>
                                <div class="carousel-control right" href="#image-additional"
                                     data-slide="next">&rsaquo;</div>
                            </div>


                            <script type="text/javascript">
                                $('#image-additional .item:first').addClass('active');
                                $('#image-additional').carousel({interval: false});

                                $(document).ready(function() {
                                    $('.colorbox').carousel({interval: false}); /* init the carousel but don't start it */
                                    var myInterval = false;
                                    $('.colorbox').mouseover(function() {
                                        var ctrl = $(this);
                                        var interval = 300;

                                        myInterval = setInterval(function() {
                                            ctrl.trigger("click");
                                        }, interval);
                                    });

                                    $('.colorbox').mouseout(function() {
                                        clearInterval(myInterval);
                                        myInterval = false;
                                    });
                                    //Examples of how to assign the Colorbox event to elements
                                    $(".colorbox").colorbox({rel: 'group1'});
                                });
                            </script>

                            <div class="box-pro">
                                <div class="show-line">
                                    Version: <span style="color:red;"><?php echo ($data['product_version'] == 1) ? "Used" : "New"; ?></span>
                                </div>
                                <div class="price-pro show-line">
                                    Price:<span class="price-new" style="color:red;"> $<?php echo $data['price']; ?></span>
                                </div>
                                <div class="viewer link  show-line">
                                    <?php echo $lang['view'] . ' : ' . $data['top_view'] ?>
                                </div>
                                <div class="viewer link">
                                    <i class="icon-envelope"></i>  <?php echo $lang['CALL_CENTER']; ?> :<a href="<?php echo W_ROOT; ?>/product/contact.php?product=<?php echo $data['id'] ?>" target="_blank"><!-- Contact Supplier--><?php echo $lang['CON_SUPPLIER']; ?></a>
                                </div>
                            </div>
                            <div class="show-line"><span style="color:black;">Contact Details</span></div>
                            <br/>
                            <div class="box-pro">
                                <p class="show-line"><span class="bold">Name:</span> <?php echo @$data['contact_name']; ?></p>
                                <p class="show-line"><span class="bold">Tel:</span> <?php echo @$data['contact_email']; ?></p>
                                <p class="show-line"><span class="bold">Email:</span> <?php echo @$data['contact_phone']; ?></p>
                                <p class="show-line"><span class="bold">Location:</span> <?php echo @$data['contact_location']; ?></p>
                                <p class="show-line"><span class="bold">Address:</span> <?php echo @$data['contact_address']; ?></p>
                            </div>
                        </div>
                        <div class="span6">
                            <?php
                            $table_pro = Tblprovince::tblprovince;
                            $province = select($table_pro, array(Tblprovince::lat_long), array(Tblprovince::id => $data[Tblproduct::province_id]));
                            $data_province = mysql_fetch_assoc($province);
                            ?>
                            <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
                            <script>
                                function initialize() {
                                    var myLatlng = new google.maps.LatLng(<?php echo $data_province[Tblprovince::lat_long]; ?>);
                                    var mapOptions = {
                                        zoom: 14,
                                        center: myLatlng
                                    }
                                    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

                                    var marker = new google.maps.Marker({
                                        position: myLatlng,
                                        map: map,
                                        title: ''
                                    });
                                }

                                google.maps.event.addDomListener(window, 'load', initialize);

                            </script>
                            <div id="map-canvas"></div>
                        </div>
                    </div>
                    <div class="row-fluid product-shop">
                        <div class="span12">
                            <div class="show-line"><span style="color:black;">Description</span></div>
                            <br/>
                            <div class="box-pro">
                                <p>
                                    <?php echo $data['product_description']; ?>
                                </p>
                            </div>
                            <br/>
                            <div class="links share">
                                <div class="wrap-share">
                                    <!-- AddThis Button BEGIN -->
                                    <div class="addthis_toolbox addthis_default_style ">
                                        <a class="addthis_button_preferred_1"></a>
                                        <a class="addthis_button_preferred_2"></a>
                                        <a class="addthis_button_preferred_3"></a>
                                        <a class="addthis_button_preferred_4"></a>
                                    </div>
                                    <!-- AddThis Button END -->
                                </div>
                            </div>
                            <div class="clear"></div>
                        </div>
                    </div>
                    <div class="clear"></div>
                    <?php include(S_ROOT . '/includefiles/sup_bottom_advertise.php'); ?>
                </div>
            </div>
        </div>
    </div>
</div>
<script
    type="text/javascript"
src="http://s7.addthis.com/js/300/addthis_widget.js#pubid=xa-5063b50f3c25bce5"></script>
<div class="clear"></div>
<div class="row-fluid">
    <?php include(S_ROOT . '/product/related_post.php'); ?>
</div>
